package top.fengleifeng.question;

/**
 * @program: leetcode-test
 * @description:
 * @author: fengleifeng
 * @create: 2020-09-30 11:44
 **/
public class Num701二叉搜索树中的插入操作 {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode() {}
     * TreeNode(int val) { this.val = val; }
     * TreeNode(int val, TreeNode left, TreeNode right) {
     * this.val = val;
     * this.left = left;
     * this.right = right;
     * }
     * }
     */
    class Solution {
        public TreeNode insertIntoBST(TreeNode root, int val) {
            if (null == root) {
                return new TreeNode(val);
            }
            TreeNode treeNode = root;
            while (treeNode != null) {
                if (treeNode.val > val) {
                    if (treeNode.left != null) {
                       treeNode = treeNode.left;
                    }else{
                        treeNode.left = new TreeNode(val);
                        break;
                    }
                }else{
                    if (treeNode.right != null) {
                        treeNode = treeNode.right;
                    }else{
                        treeNode.right = new TreeNode(val);
                        break;
                    }
                }
            }
            return root;
        }
    }


}
